﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BGCOP.CAVTOP.BUSINESSLAYER
{
    public class Vehiculos
    {
        public List<TVehiculo> ListarTodos()
        {
            masterDBACEntities db = new masterDBACEntities();
            var modv = from d in db.TVehiculo orderby d.Placa ascending select d;
            return modv.ToList();
        }
        public void asignarVehiculoEmpleado(string identEmp, string placa)
        {
            try
            {
                masterDBACEntities db = new masterDBACEntities();
                var a1 = (from a in db.TEmpleado where a.identificacion == identEmp select a).FirstOrDefault();
                var a2 = (from b in db.TVehiculo where b.Placa == placa select b).FirstOrDefault();
                var df = new TEmpleadoVehiculo()
                             {
                                IdEmpleado = a1.idEmpleado,
                                IdVehiculo = a2.idVehiculo,
                                fechaUltimaGestion = DateTime.Now,
                                usuarioUltimaGestion =1
                             };
                db.AddToTEmpleadoVehiculo(df);
                db.SaveChanges();
            }
            catch (Exception)
            {
                
                
            }
        }
        public void Agregar(string placa, int modelo, string codigoBarras, string color, 
            DateTime fechaC, int usuarioAct)
        {
            TVehiculo tmp = new TVehiculo();
            tmp.Placa = placa;
            tmp.color = color;
            tmp.idModeloVehiculo = (short)modelo;
            tmp.CodigodeBarras = codigoBarras;
            tmp.fechaUltimaGestion = fechaC;
            tmp.usuarioUltimaGestion = usuarioAct;
            try
            {
                masterDBACEntities db = new masterDBACEntities();
                db.AddToTVehiculo(tmp);
                db.SaveChanges();
            }
            catch (Exception)
            {
                
                throw;
            }
            
        }
        
        public void Agregar(string placa, string modelo, string codigoBarras, string color,
           DateTime fechaC, int usuarioAct)
        {
             
            TVehiculo tmp = new TVehiculo();
            tmp.Placa = placa;
            tmp.color = color;
            //tmp.idModeloVehiculo = (short)modelo;
            tmp.CodigodeBarras = codigoBarras;
            tmp.fechaUltimaGestion = fechaC;
            tmp.usuarioUltimaGestion = usuarioAct;
            try
            {
                masterDBACEntities db = new masterDBACEntities();
                var mobj = (from a in db.TModeloVehiculo where a.NombreModelo == modelo select a).FirstOrDefault();
                if (mobj == null)
                {
                    ModelosVehiculo md = new ModelosVehiculo();
                    md.CrearModeloVehiculo(modelo, DateTime.Now, 1);
                    var nn = (from a in db.TModeloVehiculo where a.NombreModelo == modelo select a).FirstOrDefault();
                    tmp.idModeloVehiculo = nn.idModeloVehiculo;
                    db.AddToTVehiculo(tmp);
                    db.SaveChanges();  
                }else
                {
                    tmp.idModeloVehiculo = mobj.idModeloVehiculo;
                    db.AddToTVehiculo(tmp);
                    db.SaveChanges();    
                }
                
            }
            catch (Exception)
            {

                throw;
            }

        }
        public void Modificar(int id,string placa, int modelo, string codigoBarras, string color, 
            DateTime fechaC, int usuarioAct)
        {
            TVehiculo tmp = new TVehiculo();
            tmp.Placa = placa;
            tmp.color = color;
            tmp.idModeloVehiculo = (short)modelo;
            tmp.CodigodeBarras = codigoBarras;
            tmp.fechaUltimaGestion = fechaC;
            tmp.usuarioUltimaGestion = usuarioAct;
            masterDBACEntities db = new masterDBACEntities();
            TVehiculo te = db.TVehiculo.FirstOrDefault(c => c.idVehiculo == id);
            if (te != null)
            {
                te.Placa = tmp.Placa;
                te.color = tmp.color;
                te.idModeloVehiculo = tmp.idModeloVehiculo;
                te.CodigodeBarras = tmp.CodigodeBarras;
                te.fechaUltimaGestion = tmp.fechaUltimaGestion;
                te.usuarioUltimaGestion = tmp.usuarioUltimaGestion;
            }
            db.SaveChanges();
        }
        public void Eliminar(int id)
        {
            masterDBACEntities db = new masterDBACEntities();
            TVehiculo  te = db.TVehiculo.FirstOrDefault(c => c.idVehiculo == id);
            db.TVehiculo.DeleteObject(te);
            db.SaveChanges();
        }
        public TVehiculo consultarVehiculo(string placa)
        {
            masterDBACEntities db = new masterDBACEntities();
            TVehiculo te = db.TVehiculo.FirstOrDefault(c => c.Placa  == placa);
            return te;
        }
        public string GenCodigodeBarras()
        {
            string xBar = "";
            bool res = true;
            masterDBACEntities db = new masterDBACEntities();
            while (res)
            {
                xBar = RandomString(12, false);
                var t = (from a in db.TVehiculo where a.CodigodeBarras == xBar select a).FirstOrDefault();
                if (t == null)
                {
                    res = false;
                }
                
            }
            return xBar;
        }

        public string RandomString(int size, bool lowerCase)
        {
            StringBuilder builder = new StringBuilder();
            Random random = new Random();
            char ch;
            for (int i = 0; i < size; i++)
            {
                ch = Convert.ToChar(Convert.ToInt32(Math.Floor(9*random.NextDouble() + 48)));
                builder.Append(ch);
            }
            if (lowerCase)
                return builder.ToString().ToLower();
            return builder.ToString();
        }

    }
}
